//
//  ZSStatusOriginalView.swift
//  ZSWeibo
//
//  Created by zsj1992 on 16/4/12.
//  Copyright © 2016年 ichange. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

let ZSStatusCellMargin = 5
let WeiboContentLabelFont: CGFloat = 15

class ZSStatusOriginalView: UIView {

    var bottomCons: Constraint?
    
    //原创微博视图和一个微博的视图模型打交道,从这里拿数据
    var statusViewModel:ZSStatusViewModel?{
    
        didSet{
            iconView.sd_setImageWithURL(statusViewModel?.imgIcon)
            
            mbRankImageView.image = statusViewModel?.mbImage

            verifiedImageView.image = statusViewModel?.verifiedImage

            nameLabel.text = statusViewModel?.status?.user?.name

            timeLabel.text = statusViewModel?.creatTime
            
//            print(statusViewModel?.status?.created_at)
            
            sourceLabel.text = statusViewModel?.source
            
            contentLabel.attributedText = statusViewModel?.originalAttributeString

            print(contentLabel.attributedText)
            /**
            *  有配图视图的话需要重新更新  原创微博的约束
            *
            
            */

            //配图视图设置数据源
            self.bottomCons?.uninstall()
            if let urls = statusViewModel?.status?.pic_urls where urls.count > 0 {
                //有配图视图
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(ZSStatusCellMargin).constraint
                })
                //设置数据源
                pictureView.imageURLs = urls
                //显示配图视图
                pictureView.hidden = false
            } else {
                //没有配图视图
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomCons = make.bottom.equalTo(contentLabel.snp_bottom).offset(ZSStatusCellMargin).constraint
                })
                //设置数据源 nil
//                pictureView.imageURLs = nil
                //隐藏配图视图
                pictureView.hidden = true
                
            }

        }
    
    }
    
   
    
    

    
    
    
    
    
    
    
    
    /*懒加载子控件*/
    /// 用户头像
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    /// 用户等级
    private lazy var mbRankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    /// 用户认证类型
    private lazy var verifiedImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    /// 用户名
    private lazy var nameLabel: UILabel = UILabel(text: "15期的童鞋们", textColor: UIColor.darkGrayColor(), fontSize: 15)
    /// 微博时间
    private lazy var timeLabel: UILabel = UILabel(text: "15:15", textColor: UIColor.orangeColor(), fontSize: 12)
    /// 微博来源
    private lazy var sourceLabel: UILabel = UILabel(text: "隔壁老王", textColor: UIColor.darkGrayColor(), fontSize: 12)
    /// 微博正文
    private lazy var contentLabel: UILabel = UILabel(text: "我没有男胖呀", textColor: UIColor.darkGrayColor(), fontSize: WeiboContentLabelFont)
    
    
//    private lazy var contentLabel: UILabel = UILabel(text: "我没有男胖呀", textColor: UIColor.darkGrayColor(), fontSize: WeiboContentLabelFont, margin: 5)

    //配图视图
    private lazy var pictureView:ZSStatusPictureView = ZSStatusPictureView()
    
    /**重写构造方法*/
    
    override init(frame: CGRect) {
        super.init(frame: frame)

        
        
    setupUI()

    
    
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}














extension ZSStatusOriginalView{

    func setupUI(){
    //添加所有子控件
    addSubview(iconView)
    addSubview(mbRankImageView)
    addSubview(verifiedImageView)
    addSubview(timeLabel)
    addSubview(nameLabel)
    addSubview(sourceLabel)
    addSubview(contentLabel)
    
   
    //配图视图
    addSubview(pictureView)
    
        
    //添加约束
    
    //头像
    iconView.snp_makeConstraints { (make) -> Void in

        iconView.layer.cornerRadius = 20
        make.left.top.equalTo(self).offset(ZSStatusCellMargin)
        make.size.equalTo(CGSizeMake(40, 40))
        
        
        }
    
        //名字
    nameLabel.snp_makeConstraints { (make) -> Void in
        
        make.left.equalTo(iconView.snp_right).offset(ZSStatusCellMargin)
     
        make.top.equalTo(iconView.snp_top)
        }
        
    //皇冠等级
    mbRankImageView.snp_makeConstraints { (make) -> Void in
        
        make.top.equalTo(nameLabel.snp_top)
        
        make.left.equalTo(nameLabel.snp_right).offset(ZSStatusCellMargin)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLabel.snp_left)
            make.bottom.equalTo(iconView.snp_bottom)
        }
    //认证类型
    verifiedImageView.snp_makeConstraints { (make) -> Void in
        
        make.right.equalTo(iconView.snp_right)
        
        make.bottom.equalTo(iconView.snp_bottom)
        
        }
    
     //状态来源
    sourceLabel.snp_makeConstraints { (make) -> Void in
        
        make.left.equalTo(timeLabel.snp_right).offset(ZSStatusCellMargin)
        make.bottom.equalTo(timeLabel.snp_bottom)
        
        }
        
    //正文
    contentLabel.snp_makeConstraints { (make) -> Void in
        make.top.equalTo(iconView.snp_bottom).offset(ZSStatusCellMargin)
        make.left.equalTo(self).offset(ZSStatusCellMargin)
        
//        make.width.equalTo(self.snp_width)
        
        contentLabel.numberOfLines = 0
        contentLabel.preferredMaxLayoutWidth = screenWidth - CGFloat(2*ZSStatusCellMargin)
        }

        pictureView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentLabel.snp_left)
            make.top.equalTo(contentLabel.snp_bottom).offset(ZSStatusCellMargin)
            make.size.equalTo(CGSize(width: 100, height: 100))
        }

      
        //设置自己的约束-->很多时候都是自己这个父视图没有进行设置导致视图显示不正确
        self.snp_makeConstraints { (make) -> Void in
           self.bottomCons = make.bottom.equalTo(pictureView.snp_bottom).constraint
        }

        
    }
}











